from clients import clients_lst
import time
import random
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


s = Service(executable_path="./chromedriver")
driver = webdriver.Chrome(service=s)


def send_sms(name: str, phone: str) -> None:
    current_month = str(datetime.now().date())[5:7]
    new_sms_btn = driver.find_element(By.ID, "smslist-new-sms")

    new_sms_btn.click()

    time.sleep(5)

    phones_input = driver.find_element(By.ID, "chosen-search-field-input")
    sms_body = driver.find_element(By.ID, "chat-input")
    send_sms_btn = driver.find_element(By.ID, "btn-send")

    phones_input.clear()
    phones_input.send_keys(phone)
    phones_input.send_keys(Keys.ENTER)
    sms_body.clear()
    sms_body.send_keys(
        f"itRoom школа программирования.\nЗдравствуйте, {name}, пожалуйста оплатите обучение до 10.{current_month}. "
        f"Не получается вовремя оплатить? Свяжитесь с нами и мы сделаем отсрочку.\nЭто автоматическое письмо от "
        f"робота - напоминатора, созданного в стенах нашей школы, отвечать на сообщение не нужно.\nСвязь с нами: "
        f"8-993-573-37-53"
    )
    send_sms_btn.click()


def del_sms_list():
    checkbox_check_all_sms = driver.find_element(By.ID, "smslist-checkAll")
    btn_delete_all_sms = driver.find_element(By.ID, "smslist-delete")
    btn_confirm_delete_all_sms = driver.find_element(By.ID, "yesbtn")

    checkbox_check_all_sms.click()
    btn_delete_all_sms.click()
    btn_confirm_delete_all_sms.click()


def main():
    driver.get("http://192.168.0.1/index.html#sms")
    try:
        driver.start_client()
        for client in clients_lst:
            name, phone = client
            time.sleep(10)
            send_sms(name, phone)
            time.sleep(random.randint(5, 20))
            driver.refresh()
            # else:
            #     del_sms_list()
            #     time.sleep(2)
            #     send_sms(name, phone)
            #     time.sleep(20)

        time.sleep(20)

    except Exception as ex:
        print(f"exception -->{ex}")
    finally:
        driver.close()
        driver.quit()
        print("")


if __name__ == "__main__":
    main()
